package com.ermao.mymes.handler.service.discard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ermao.mymes.common.constant.MyMesConstant;
import com.ermao.mymes.common.domain.AnchorInfo;
import com.ermao.mymes.common.domain.TaskInfo;
import com.ermao.mymes.common.enums.AnchorState;
import com.ermao.mymes.support.utils.LogUtils;
import org.springframework.stereotype.Service;

/**
 * @author Ermao
 * Date: 2022/5/9 16:34
 */
@Service
public class DiscardMessageService {
	private static final String DISCARD_MESSAGE_KEY = "discard";

	@ApolloConfig("dev.mymes")
	private Config config;

	/**
	 * 丢弃消息，配置在apollo
	 * @param taskInfo
	 * @return
	 */
	public boolean isDiscard(TaskInfo taskInfo) {
		// 配置示例:	["1","2"]
		JSONArray array = JSON.parseArray(config.getProperty(DISCARD_MESSAGE_KEY,
				MyMesConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY));
		if (array.contains(String.valueOf(taskInfo.getMessageTemplateId()))) {
			LogUtils.print(AnchorInfo.builder().businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).state(AnchorState.DISCARD.getCode()).build());
			return true;
		}
		return false;
	}
}
